<?php
declare (strict_types = 1);

namespace app\command;


use app\api\controller\BannerType;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;

class Task extends Command
{
    //https://www.phpdie.com/php-3-188.html
    //https://gitee.com/392223903/EasyTask#https://gitee.com/link?target=https%3A%2F%2Fwww.gaojiufeng.cn%2F%3Fid%3D328
    protected function configure()
    {
        //设置名称为task
        $this->setName('task')
            //增加一个命令参数
            ->addArgument('action', Argument::OPTIONAL, "action", '')
            ->addArgument('force', Argument::OPTIONAL, "force", '');
    }

    protected function execute(Input $input, Output $output)
    {
        //获取输入参数
        $action = trim($input->getArgument('action'));
        $force = trim($input->getArgument('force'));

        // 配置任务，每隔20秒访问2次网站
        $task = new \EasyTask\Task();
        $task->setDaemon(true);

        $task->setRunTimePath('./runtime/');
//        $task->addFunc(function () {
//
//            $url = 'http://121.41.177.54:801/api/notice/index';
//            file_get_contents($url);
//        }, 'request2', 1, 2);;//每1秒执行5次
        $taskList = config('task');
        $task->addClass('\app\\api\\controller\\BannerType', 'all', 'abadf', 1, 1);

        foreach ($taskList as $v) {
            $task->addClass('app\\command\\' . $v[0], 'handle', $v[0], $v[1], $v[2]);
        }
        // 根据命令执行
        if ($action == 'start')
        {
            $task->start();
        }
        elseif ($action == 'status')
        {
            $task->status();
        }
        elseif ($action == 'stop')
        {
            $force = ($force == 'force'); //是否强制停止
            $task->stop($force);
        }
        else
        {
            exit('Command is not exist');
        }
    }
}
